package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.Service.FlowerTypeService;
import com.woniu.annotation.CheckPermission;
import com.woniu.entity.FlowerType;
import com.woniu.util.JsonResult;
import com.woniu.vo.FlowerTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: Gwj
 * @Date: 2024/7/8 15:14
 */
@RestController
@Slf4j
@RequestMapping("/flowerType")
@Api(value = "鲜花类别功能", tags = "所有的鲜花类别功能")
public class FlowerTypeController {
    @Resource
    private FlowerTypeService flowerTypeService;

    /**
     * 鲜花分类，分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/findFlowerTypeByPage")
    @ApiOperation(value = "鲜花分类分页查询接口")
    @CheckPermission("type:select")
    public JsonResult<PageInfo<FlowerTypeVo>> findFlowerTypeByPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) throws Exception {
        PageInfo<FlowerTypeVo> allFlowerTypes = flowerTypeService.findAllFlowerTypes(pageNum, pageSize);
        return JsonResult.build(200, "success", allFlowerTypes);
    }

    /**
     * 模糊查询并且分页
     *
     * @param pageNum
     * @param pageSize
     * @param typeName
     * @param isDelete
     * @return
     * @throws Exception
     */
    @GetMapping("/findFlowerTypeByConditions")
    @ApiOperation(value = "根据条件查询鲜花类别")
    @CheckPermission("type:select")
    public JsonResult<PageInfo<FlowerTypeVo>> findFlowerTypeByConditions(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String typeName, Integer isDelete) throws Exception {
        FlowerTypeVo flowerTypeVo = new FlowerTypeVo();
        flowerTypeVo.setTypeName(typeName);
        flowerTypeVo.setIsDelete(isDelete);
        PageInfo<FlowerTypeVo> flowerTypesByConditions = flowerTypeService.findFlowerTypesByConditions(flowerTypeVo, pageNum, pageSize);
        return JsonResult.ok(200, flowerTypesByConditions);
    }

    /**
     * 删除鲜花， 逻辑删除
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @PutMapping("/deleteFlowerType")
    @ApiOperation(value = "删除鲜花类别的接口")
    @CheckPermission("type:delete")
    public JsonResult deleteFlowerType(FlowerType flowerType) throws Exception {
        int i = flowerTypeService.changeFlowerTypeIsDeleted(flowerType);
        if (i > 0) {
            return JsonResult.build(200, "删除成功", i);
        } else {
            return JsonResult.build(500, "删除失败", i);
        }
    }

    /**
     * 新增鲜花的分类
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @PostMapping("/addFlowerType")
    @ApiOperation(value = "添加鲜花类别的接口")
    @CheckPermission("type:insert")
    public JsonResult addFlowerType(FlowerType flowerType) throws Exception {
        int i = flowerTypeService.addFlowerType(flowerType);
        if (i > 0) {
            return JsonResult.build(200, "添加成功", i);
        } else {
            return JsonResult.build(500, "添加失败", i);
        }
    }

    /**
     * 修改鲜花分类信息
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @PutMapping("/updateFlowerType")
    @ApiOperation(value = "修改鲜花类别的接口")
    @CheckPermission("type:update")
    public JsonResult updateFlowerType(FlowerType flowerType) throws Exception {
        int i = flowerTypeService.updateFlowerTypeInfoById(flowerType);
        if (i > 0) {
            return JsonResult.build(200, "修改成功", i);
        } else {
            return JsonResult.build(500, "修改失败", i);
        }
    }

    /**
     * 禁用鲜花分类
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @PutMapping("/disableFlowerType")
    @ApiOperation(value = "禁用花类别的接口")
    @CheckPermission("type:update")
    public JsonResult disableFlowerType(FlowerType flowerType) throws Exception {
        int i = flowerTypeService.disableFlowerType(flowerType);
        if (i > 0) {
            return JsonResult.build(200, "禁用成功", i);
        } else {
            return JsonResult.build(500, "禁用失败", i);
        }
    }

    /**
     * 新增时的重名验证
     *
     * @param typeName
     * @return
     * @throws Exception
     */
    @GetMapping("/checkFlowerTypeName/{typeName}")
    @ApiOperation(value = "检查花类别名称是否存在的接口")
    public JsonResult checkFlowerTypeName(@PathVariable String typeName) throws Exception {
        FlowerType flowerType = flowerTypeService.checkFlowerTypeName(typeName);
        if (flowerType != null) {
            if (flowerType.getTypeName().equals(typeName)) {
                return JsonResult.build(500, "此花类别名称已存在，不可使用", false);
            } else {
                return JsonResult.build(200, "此花类别名称可以使用", true);
            }
        } else {
            return JsonResult.build(200, "此花类别名称可以使用", true);
        }
    }

    @GetMapping("/checkFlowerTypeNameAndId/{typeName}/{typeId}")
    @ApiOperation(value = "检查花类别名称是否存在且不与自身id相同的接口")
    public JsonResult checkFlowerTypeNameAndId(@PathVariable String typeName, @PathVariable Integer typeId) throws Exception {
        FlowerType flowerType = flowerTypeService.selectFlowerTypeNameAndId(typeName, typeId);
        if (flowerType == null) {
            return JsonResult.build(200, "此花类别名称可以使用", true);
        } else {
            return JsonResult.build(500, "此花类别名称已存在，不可使用", false);
        }
    }

}
